package com.imooc.disruptor.server;

import com.imooc.disruptor.disruptor.MessageProducer;
import com.imooc.disruptor.disruptor.RingBufferWorkerPoolFactory;
import com.imooc.disruptor.entity.TranslatorData;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yqc 2018/10/15
 */
@Slf4j
public class ServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        /*TranslatorData request = (TranslatorData) msg;
        log.info("Server端: id = {}, name = {}, message = {}",
                request.getId(), request.getName(), request.getMessage());
        TranslatorData response = new TranslatorData();
        response.setId("resp: " + request.getId());
        response.setName("resp: " + request.getName());
        response.setMessage("resp: " + request.getMessage());
        ctx.writeAndFlush(response);*/
        TranslatorData request = (TranslatorData) msg;
        //自己的应用服务应该有一个ID生成规则
        String producerId = "sessionId:001";
        MessageProducer messageProducer = RingBufferWorkerPoolFactory.getInstance().getMessageProducer(producerId);
        messageProducer.onData(request, ctx);
    }
}
